/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package fr.ove.openmath.mathematica;

/**
 *
 * @author Mr.Sam
 */
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
import java.awt.*;
import java.util.*;
import java.beans.PropertyVetoException;

public class MyJInternalFrame extends JInternalFrame implements Savable {
    /**
    * The desktop which will contain the instance
    */
    Desktop desktop;
    
    /**
    * The Constructor.
    * @param desktop the desktop which will contain the instance.
    */
    public MyJInternalFrame(Desktop desktop) {
        super();
        this.desktop = desktop;
    }
        
    /**
    * The Constructor.
    * @param title the title of the instance.
    * @param desktop the desktop which will contain the instance.
    */
    public MyJInternalFrame(String title, Desktop desktop) {
        super(title);
        this.desktop = desktop;
    }
    
    /**
    * Specifies if the instance has its maximum size (i.e. occupies all the desktop
    * space).
    * @param max <CODE>true</CODE> if we want the instance to be maximum. <CODE>false</CODE>
    * otherwise.
    */
    public void setMaximum(boolean max) throws PropertyVetoException {
        if (isMaximum() != max) {
            super.setMaximum(max);
            desktop.setMaximize(max);
            desktop.setAllMaximum();
        }
        else
            return;
            
        // Sets the current instance as selected.
        // Its it which had received
        try {
            this.setSelected(true);
        } catch (PropertyVetoException pve) {
            pve.printStackTrace();
        }
    }
            
    // ### Savable interface implementation
        
    /**
    * Saves the instance.
    * @returns the result of the save action. Should be <CODE>null</CODE> if nothing
    * is expected after the save operation. The saved object otherwise.
    * The instance is saved as the concatenation of all the OpenMath object request contained.
    */
    public Object save() {
        StringBuffer buffer = new StringBuffer();
        buffer.append(((Savable) getContentPane()).save());
            
        try {
            FileWriter fw = new FileWriter(getTitle() + ".wks");
            fw.flush();
            fw.write(buffer.toString(), 0, buffer.length());
            fw.close();
        }
        catch (IOException ioe) {
            System.out.println("Impossible to create the file : " + getTitle() + ".wks");
        }
            
                
        return null;
    }
    
    /**
    * Saves the instance into the specified file.
    * @returns the result of the save action. Should be <CODE>null</CODE> if nothing
    * is expected after the save operation. The saved object otherwise.
    */
    public Object save(File file) {
        StringBuffer buffer = new StringBuffer();
        buffer.append(((Savable) getContentPane()).save());
            
        try {
            FileWriter fw = new FileWriter(file);
            fw.flush();
            fw.write(buffer.toString(), 0, buffer.length());
            fw.close();
        }
        catch (IOException ioe) {
            System.out.println("Impossible to write into the file : " + file.getPath() );
        }
                
        return null;
    }
    /**
    * Saves the instance with the specified name.
    * @param name the specified name.
    * @returns the result of the save action. Should be <CODE>null</CODE> if nothing
    * is expected after the save operation. The saved object otherwise.
    */
    public Object saveAs(String name) {
        return null;
    }
        
    /**
    * Checks if the instance need to be saved.
    * @return <CODE>true</CODE> if the instance need to be saved. <CODE>false</CODE> otherwise.
    */
    public boolean isSaveNeeded() {
        // The instance need be saved if its content need be saved.
        return ((Savable) getContentPane()).isSaveNeeded();
    }
        
    /**
    * Sets if the instance need to be saved.
    * @param needed <CODE>true</CODE> if the instance need to be saved. <CODE>false</CODE> otherwise.
    */
    public void setSaveNeeded(boolean needed) {
        // If the instance need be saved, then its content need be saved.
        ((Savable) getContentPane()).setSaveNeeded(needed);
    }
}

